home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Applications / gdbm-1.7.3 / testgdbm.c < prev    next >
Text File  |  1994-05-22  |  14KB  |  519 lines

  1. /* testgdbm.c - Driver program to test the database routines and to
  2.    help debug gdbm.  Uses inside information to show "system" information */
  3.  
  4. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  5.     Copyright (C) 1990, 1991, 1993  Free Software Foundation, Inc.
  6.  
  7.     GDBM is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.  
  12.     GDBM is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with GDBM; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.     You may contact the author by:
  22.        e-mail:  phil@cs.wwu.edu
  23.       us-mail:  Philip A. Nelson
  24.                 Computer Science Department
  25.                 Western Washington University
  26.                 Bellingham, WA 98226
  27.        
  28. *************************************************************************/
  29.  
  30.  
  31. #ifdef THINK_C
  32. #include <console.h>
  33. #include <stdlib.h>
  34. #define  alloca(m)  malloc(m)
  35. #else
  36. /* AIX demands this be the very first thing in the file. */
  37. #if !defined(__GNUC__) && defined(_AIX)
  38.  #pragma alloca
  39. #endif
  40. #endif
  41.  
  42. /* include system configuration before all else. */
  43. #include "autoconf.h"
  44.  
  45. #include "gdbmdefs.h"
  46. #include "gdbmerrno.h"
  47. #include "extern.h"
  48.  
  49. #include "getopt.h"
  50.  
  51. extern const char * gdbm_version;
  52.  
  53. extern const char *gdbm_strerror _ARGS((gdbm_error));
  54.  
  55. gdbm_file_info *gdbm_file;
  56.  
  57. /* Debug procedure to print the contents of the current hash bucket. */
  58. void
  59. print_bucket (bucket, mesg)
  60.      hash_bucket *bucket;
  61.      char *mesg;
  62. {
  63.   int  index;
  64.  
  65.   printf ("******* %s **********\n\nbits = %d\ncount= %d\nHash Table:\n",
  66.      mesg, bucket->bucket_bits, bucket->count);
  67.   printf ("     #    hash value     key size    data size     data adr  home\n");
  68.   for (index = 0; index < gdbm_file->header->bucket_elems; index++)
  69.     printf ("  %4d  %12x  %11d  %11d  %11d %5d\n", index,
  70.        bucket->h_table[index].hash_value,
  71.        bucket->h_table[index].key_size,
  72.        bucket->h_table[index].data_size,
  73.        bucket->h_table[index].data_pointer,
  74.        bucket->h_table[index].hash_value % gdbm_file->header->bucket_elems);
  75.  
  76.   printf ("\nAvail count = %1d\n", bucket->av_count);
  77.   printf ("Avail  adr     size\n");
  78.   for (index = 0; index < bucket->av_count; index++)
  79.     printf ("%9d%9d\n", bucket->bucket_avail[index].av_adr,
  80.                     bucket->bucket_avail[index].av_size);
  81. }
  82.  
  83.  
  84. void
  85. _gdbm_print_avail_list (dbf)
  86.      gdbm_file_info *dbf;
  87. {
  88.   int temp;
  89.   int size;
  90.   avail_block *av_stk;
  91.  
  92.   /* Print the the header avail block.  */
  93.   printf ("\nheader block\nsize  = %d\ncount = %d\n",
  94.       dbf->header->avail.size, dbf->header->avail.count);
  95.   for (temp = 0; temp < dbf->header->avail.count; temp++)
  96.     {
  97.       printf ("  %15d   %10d \n", dbf->header->avail.av_table[temp].av_size,
  98.           dbf->header->avail.av_table[temp].av_adr);
  99.     }
  100.  
  101.   /* Initialize the variables for a pass throught the avail stack. */
  102.   temp = dbf->header->avail.next_block;
  103.   size = ( ( (dbf->header->avail.size * sizeof (avail_elem)) >> 1)
  104.       + sizeof (avail_block));
  105.   av_stk = (avail_block *) alloca (size);
  106.  
  107.   /* Print the stack. */
  108.   while (FALSE)
  109.     {
  110.       lseek (dbf->desc, temp, L_SET);
  111.       read  (dbf->desc, av_stk, size);
  112.  
  113.       /* Print the block! */
  114.       printf ("\nblock = %d\nsize  = %d\ncount = %d\n", temp,
  115.           av_stk->size, av_stk->count);
  116.       for (temp = 0; temp < av_stk->count; temp++)
  117.     {
  118.       printf ("  %15d   %10d \n", av_stk->av_table[temp].av_size,
  119.         av_stk->av_table[temp].av_adr);
  120.     }
  121.       temp = av_stk->next_block;
  122.     }
  123. }
  124.  
  125. void
  126. _gdbm_print_bucket_cache (dbf)
  127.      gdbm_file_info *dbf;
  128. {
  129.   register int index;
  130.   char changed;
  131.  
  132.   if (dbf->bucket_cache != NULL) {
  133.     printf(
  134.       "Bucket Cache (size %d):\n  Index:  Address  Changed  Data_Hash \n",
  135.       dbf->cache_size);
  136.     for (index=0; index < dbf->cache_size; index++) {
  137.       changed = dbf->bucket_cache[index].ca_changed;
  138.       printf ("  %5d:  %7d  %7s  %x\n",
  139.           index,
  140.           dbf->bucket_cache[index].ca_adr,
  141.           (changed ? "True" : "False"),
  142.           dbf->bucket_cache[index].ca_data.hash_val);
  143.     }
  144.   } else
  145.     printf("Bucket cache has not been initialized.\n");
  146. }
  147.  
  148. void
  149. usage (s)
  150.      char *s;
  151. {
  152.   printf(
  153.       "Usage: %s [-r or -nf] [-b block-size] [-c cache-size] [-g gdbm-file]\n",
  154.       s);
  155.   exit (2);
  156. }
  157.  
  158.  
  159. /* The test program allows one to call all the routines plus the hash function.
  160.    The commands are single letter commands.  The user is prompted for all other
  161.    information.  See the help command (?) for a list of all commands. */
  162.  
  163. int
  164. main (argc, argv)
  165.      int argc;
  166.      char *argv[];
  167.  
  168. {
  169.  
  170.   char  cmd_ch;
  171.  
  172.   datum key_data;
  173.   datum data_data;
  174.   datum return_data;
  175.  
  176.   char key_line[500];
  177.   char data_line[1000];
  178.  
  179.   char done = FALSE;
  180.   int  opt;
  181.   char reader = FALSE;
  182.   char newdb = FALSE;
  183.   int  fast  = 0;
  184.  
  185.   int  cache_size = DEFAULT_CACHESIZE;
  186.   int  block_size = 0;
  187.  
  188.   char *file_name = NULL;
  189.  
  190. #ifdef THINK_C
  191.   argc = ccommand(&argv);
  192. #endif
  193.  
  194.  
  195.   /* Argument checking. */
  196.   opterr = 0;
  197.   while ((opt = getopt (argc, argv, "frnc:b:g:")) != -1)
  198.     switch (opt) {
  199.     case 'f':  fast = GDBM_FAST;
  200.                if (reader) usage (argv[0]);
  201.                break;
  202.     case 'r':  reader = TRUE;
  203.                if (newdb) usage (argv[0]);
  204.                break;
  205.     case 'n':  newdb = TRUE;
  206.                if (reader) usage (argv[0]);
  207.                break;
  208.     case 'c':  cache_size = atoi(optarg);
  209.                break;
  210.     case 'b':  block_size = atoi(optarg);
  211.                break;
  212.     case 'g':  file_name = optarg;
  213.                break;
  214.     default:  usage(argv[0]);
  215.     }
  216.  
  217.   if(file_name == NULL) 
  218.     file_name = "junk.gdbm";
  219.  
  220.   /* Initialize variables. */
  221.   key_data.dptr = NULL;
  222.   data_data.dptr = data_line;
  223.  
  224.   if (reader)
  225.     {
  226.       gdbm_file = gdbm_open (file_name, block_size, GDBM_READER, 00664, NULL);
  227.     }
  228.   else if (newdb)
  229.     {
  230.       gdbm_file =
  231.         gdbm_open (file_name, block_size, GDBM_NEWDB | fast, 00664, NULL);
  232.     }
  233.   else
  234.     {
  235.       gdbm_file =
  236.         gdbm_open (file_name, block_size, GDBM_WRCREAT | fast, 00664, NULL);
  237.     }
  238.   if (gdbm_file == NULL)
  239.     {
  240.       printf("gdbm_open failed, %s\n", gdbm_strerror(gdbm_errno));
  241.       exit (2);
  242.     }
  243.  
  244.   if (gdbm_setopt(gdbm_file, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
  245.     {
  246.       printf("gdbm_setopt failed, %s\n", gdbm_strerror(gdbm_errno));
  247.       exit(2);
  248.     }
  249.  
  250.   /* Welcome message. */
  251.   printf ("\nWelcome to the gdbm test program.  Type ? for help.\n\n");
  252.   
  253.   while (!done)
  254.     {
  255.       printf ("com -> ");
  256.       cmd_ch = getchar ();
  257.       if (cmd_ch != '\n')
  258.     {
  259.       char temp;
  260.       do
  261.           temp = getchar ();
  262.       while (temp != '\n' && temp != EOF);
  263.     }
  264.       if (cmd_ch == EOF) cmd_ch = 'q';
  265.       switch (cmd_ch)
  266.     {
  267.     
  268.     /* Standard cases found in all test{dbm,ndbm,gdbm} programs. */
  269.     case '\n':
  270.       printf ("\n");
  271.       break;
  272.  
  273.     case 'c':
  274.       {
  275.         int temp;
  276.         temp = 0;
  277.         if (key_data.dptr != NULL) free (key_data.dptr);
  278.         return_data = gdbm_firstkey (gdbm_file);
  279.         while (return_data.dptr != NULL)
  280.           {
  281.         temp++;
  282.         key_data = return_data;
  283.         return_data = gdbm_nextkey (gdbm_file, key_data);
  284.         free (key_data.dptr);
  285.           }
  286.         printf ("There are %d items in the database.\n\n", temp);
  287.         key_data.dptr = NULL;
  288.       }
  289.       break;
  290.  
  291.     case 'd':
  292.       if (key_data.dptr != NULL) free (key_data.dptr);
  293.       printf ("key -> ");
  294.       gets (key_line);
  295.       key_data.dptr = key_line;
  296.       key_data.dsize = strlen (key_line)+1;
  297.       if (gdbm_delete (gdbm_file, key_data) != 0)
  298.         printf ("Item not found or deleted\n");
  299.       printf ("\n");
  300.       key_data.dptr = NULL;
  301.       break;
  302.  
  303.     case 'f':
  304.       if (key_data.dptr != NULL) free (key_data.dptr);
  305.       printf ("key -> ");
  306.       gets (key_line);
  307.       key_data.dptr = key_line;
  308.       key_data.dsize = strlen (key_line)+1;
  309.       return_data = gdbm_fetch (gdbm_file, key_data);
  310.       if (return_data.dptr != NULL)
  311.         {
  312.           printf ("data is ->%s\n\n", return_data.dptr);
  313.           free (return_data.dptr);
  314.         }
  315.       else
  316.         printf ("No such item found.\n\n");
  317.       key_data.dptr = NULL;
  318.       break;
  319.  
  320.     case 'n':
  321.       if (key_data.dptr != NULL) free (key_data.dptr);
  322.       printf ("key -> ");
  323.       gets (key_line);
  324.       key_data.dptr = key_line;
  325.       key_data.dsize = strlen (key_line)+1;
  326.       return_data = gdbm_nextkey (gdbm_file, key_data);
  327.       if (return_data.dptr != NULL)
  328.         {
  329.           key_data = return_data;
  330.           printf ("key is  ->%s\n", key_data.dptr);
  331.           return_data = gdbm_fetch (gdbm_file, key_data);
  332.           printf ("data is ->%s\n\n", return_data.dptr);
  333.           free (return_data.dptr);
  334.         }
  335.       else
  336.         {
  337.           printf ("No such item found.\n\n");
  338.           key_data.dptr = NULL;
  339.         }
  340.       break;
  341.  
  342.     case 'q':
  343.       done = TRUE;
  344.       break;
  345.  
  346.     case 's':
  347.       if (key_data.dptr != NULL) free (key_data.dptr);
  348.       printf ("key -> ");
  349.       gets (key_line);
  350.       key_data.dptr = key_line;
  351.       key_data.dsize = strlen (key_line)+1;
  352.       printf ("data -> ");
  353.       gets (data_line);
  354.       data_data.dsize = strlen (data_line)+1;
  355.       if (gdbm_store (gdbm_file, key_data, data_data, GDBM_REPLACE) != 0)
  356.         printf ("Item not inserted. \n");
  357.       printf ("\n");
  358.       key_data.dptr = NULL;
  359.       break;
  360.  
  361.     case '1':
  362.       if (key_data.dptr != NULL) free (key_data.dptr);
  363.       key_data = gdbm_firstkey (gdbm_file);
  364.       if (key_data.dptr != NULL)
  365.         {
  366.           printf ("key is  ->%s\n", key_data.dptr);
  367.           return_data = gdbm_fetch (gdbm_file, key_data);
  368.           printf ("data is ->%s\n\n", return_data.dptr);
  369.           free (return_data.dptr);
  370.         }
  371.       else
  372.         printf ("No such item found.\n\n");
  373.       break;
  374.  
  375.     case '2':
  376.       return_data = gdbm_nextkey (gdbm_file, key_data);
  377.       if (return_data.dptr != NULL)
  378.         {
  379.           free (key_data.dptr);
  380.           key_data = return_data;
  381.           printf ("key is  ->%s\n", key_data.dptr);
  382.           return_data = gdbm_fetch (gdbm_file, key_data);
  383.           printf ("data is ->%s\n\n", return_data.dptr);
  384.           free (return_data.dptr);
  385.         }
  386.       else
  387.         printf ("No such item found.\n\n");
  388.       break;
  389.  
  390.  
  391.     /* Special cases for the testgdbm program. */
  392.     case 'r':
  393.       {
  394.         if (gdbm_reorganize (gdbm_file))
  395.           printf ("Reorganization failed. \n\n");
  396.         else
  397.           printf ("Reorganization succeeded. \n\n");
  398.       }
  399.       break;
  400.  
  401.     case 'A':
  402.       _gdbm_print_avail_list (gdbm_file);
  403.       printf ("\n");
  404.       break;
  405.  
  406.     case 'B':
  407.       {
  408.         int temp;
  409.         char number[80];
  410.  
  411.         printf ("bucket? ");
  412.         gets (number);
  413.         sscanf (number,"%d",&temp);
  414.  
  415.         if (temp >= gdbm_file->header->dir_size /4)
  416.           {
  417.         printf ("Not a bucket. \n\n");
  418.         break;
  419.           }
  420.         _gdbm_get_bucket (gdbm_file, temp);
  421.       }
  422.       printf ("Your bucket is now ");
  423.  
  424.     case 'C':
  425.       print_bucket (gdbm_file->bucket, "Current bucket");
  426.       printf ("\n current directory entry = %d.\n", gdbm_file->bucket_dir);
  427.       printf (" current bucket address  = %d.\n\n",
  428.           gdbm_file->cache_entry->ca_adr);
  429.       break;
  430.  
  431.     case 'D':
  432.       printf ("Hash table directory.\n");
  433.       printf ("  Size =  %d.  Bits = %d. \n\n",gdbm_file->header->dir_size,
  434.           gdbm_file->header->dir_bits);
  435.       {
  436.         int temp;
  437.  
  438.         for (temp = 0; temp < gdbm_file->header->dir_size / 4; temp++)
  439.           {
  440.         printf ("  %10d:  %12d\n", temp, gdbm_file->dir[temp]);
  441.         if ( (temp+1) % 20 == 0 && isatty (0))
  442.           {
  443.             printf ("*** CR to continue: ");
  444.             while (getchar () != '\n') /* Do nothing. */;
  445.           }
  446.           }
  447.       }
  448.       printf ("\n");
  449.       break;
  450.  
  451.     case 'F':
  452.       {
  453.         printf ("\nFile Header: \n\n");
  454.         printf ("  table        = %d\n", gdbm_file->header->dir);
  455.         printf ("  table size   = %d\n", gdbm_file->header->dir_size);
  456.         printf ("  table bits   = %d\n", gdbm_file->header->dir_bits);
  457.         printf ("  block size   = %d\n", gdbm_file->header->block_size);
  458.         printf ("  bucket elems = %d\n", gdbm_file->header->bucket_elems);
  459.         printf ("  bucket size  = %d\n", gdbm_file->header->bucket_size);
  460.         printf ("  header magic = %x\n", gdbm_file->header->header_magic);
  461.         printf ("  next block   = %d\n", gdbm_file->header->next_block);
  462.         printf ("  avail size   = %d\n", gdbm_file->header->avail.size);
  463.         printf ("  avail count  = %d\n", gdbm_file->header->avail.count);
  464.         printf ("  avail nx blk = %d\n", gdbm_file->header->avail.next_block);
  465.         printf ("\n");
  466.       }
  467.       break;
  468.  
  469.         case 'H':
  470.       if (key_data.dptr != NULL) free (key_data.dptr);
  471.       printf ("key -> ");
  472.       gets (key_line);
  473.       key_data.dptr = key_line;
  474.       key_data.dsize = strlen (key_line)+1;
  475.       printf ("hash value = %x. \n\n", _gdbm_hash (key_data));
  476.       key_data.dptr = NULL;
  477.       break;
  478.  
  479.     case 'K':
  480.       _gdbm_print_bucket_cache (gdbm_file);
  481.       break;
  482.  
  483.     case 'V':
  484.       printf ("%s\n\n", gdbm_version);
  485.       break;
  486.  
  487.     case '?':
  488.       printf ("c - count (number of entries)\n");
  489.       printf ("d - delete\n");
  490.       printf ("f - fetch\n");
  491.       printf ("n - nextkey\n");
  492.       printf ("q - quit\n");
  493.       printf ("s - store\n");
  494.       printf ("1 - firstkey\n");
  495.       printf ("2 - nextkey on last key (from n, 1 or 2)\n\n");
  496.  
  497.       printf ("r - reorganize\n");
  498.       printf ("A - print avail list\n");
  499.       printf ("B - get and print current bucket n\n");
  500.       printf ("C - print current bucket\n");
  501.       printf ("D - print hash directory\n");
  502.       printf ("F - print file header\n");
  503.       printf ("H - hash value of key\n");
  504.       printf ("K - print the bucket cache\n");
  505.       printf ("V - print version of gdbm\n");
  506.       break;
  507.  
  508.     default:
  509.       printf ("What? \n\n");
  510.       break;
  511.  
  512.     }
  513.     }
  514.  
  515.   /* Quit normally. */
  516.   exit (0);
  517.  
  518. }
  519.